﻿using System.Collections.Generic;
using System.Linq;
using Bll;
using Entity;
using System.Web.Mvc;
using Vote.Models;
using System.Net;
using System.Text.RegularExpressions;

namespace Vote.Controllers
{
    public class TestController:BaseController
    {
        /// <summary>
        /// 投票页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Votes(int VoteNumberId = 0, string UserName = "")
        {
            if (CurrentUser == null || CurrentUser.UserName == "")
            {
                UserInfoModel um = new UserInfoModel();
                um.UserName = UserName;
                CurrentUser = um;
            }
            var redisClient = RedisManager.GetClient();
            var VoteShows = redisClient.GetTypedClient<VoteShowModel>();
            List<VoteShowModel> ListVoteShow = new List<VoteShowModel>();
            List<VoteShowModel> VoteModel = new List<VoteShowModel>();
            ListVoteShow = VoteShows.GetAll().ToList();
            string sql = "select Identify,Id,Name,ImgUrl,VoteNum from VoteShow where 1=1 ";
            if (VoteNumberId == 0)
            {
                //默认查询所有
            }
            else
            {
                sql += "and VoteNumberId='" + VoteNumberId + "'";
            }

            List<VoteShowModel> votes = BllVoteNumber.SelectEntityBySql<VoteShowModel>("VoteShow", sql);
            //从数据库读取票数放到moodel里面
            if (ListVoteShow.Count > 0 && votes.Count > 0)
            {
                foreach (var listvote in ListVoteShow)
                {
                    foreach (var voteDb in votes)
                    {
                        if (voteDb.Id == listvote.Id)
                        {
                            listvote.VoteNum = voteDb.VoteNum;
                            VoteModel.Add(listvote);
                        }
                    }
                }
            }
            ViewBag.Model = VoteModel;
            if (Request.IsAjaxRequest())
            {
                return PartialView("~/Views/Test/TestPlayer.cshtml", VoteModel);
            }
            else
            {
                return View();
            }
            return View();
        }

        #region Redis插入
        /// <summary>
        /// 投票记录表插入redis
        /// </summary>
        public void InsertVideoUrl() {
            string sql = "select ShowId,Id,VideoUrl,VideoImageUrl from VoteVideoUrl";
            RedisCommon.InsertListToRedis<VoteVideoUrl>(sql, "VoteVideoUrl");      
        }
        public void InsertIntoRedids() {
            var redisClient = RedisManager.GetClient();
            var user = redisClient.GetTypedClient<VoteRecord>();
            List<VoteRecord> rediss = new List<VoteRecord>();
            List<VoteRecord> ss = BllVoteNumber.SelectEntityBySql<VoteRecord>("VoteRecord", "select * from VoteRecord");
            foreach(var s in ss){
                //插入数据
            var RedisUser = new VoteRecord
            {
                Id = user.GetNextSequence(),
                ShowId=s.ShowId,
            };
            var userToStore = new List<VoteRecord> { RedisUser };
            user.StoreAll(userToStore);
            }
        }

        /// <summary>
        /// 初始化需要执行的方法
        /// </summary>
        public void InsertListToRedis() {
            string sql = "select Identify,Id,Name,ImgUrl from VoteShow where IsDelete=0";
            RedisCommon.InsertListToRedis<VoteShowModel>(sql, "VoteShow");
        }

        /// <summary>
        /// Redis插入VoteNumber
        /// </summary>
        public void InsertVoteNumberToRedis()
        {
            string sql="select [Id],[Name],[StartTime],[EndTime],[Num],[CreateTime],[IsEffective] from  VoteNumber where IsEffective=1";
            RedisCommon.InsertListToRedis<VoteNumber>(sql, "VoteNumber");
        }
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        public ActionResult GetData(string message)
        {
            VoteShowModel v = new VoteShowModel();
            v.Name = message;
            return PartialView(v);
        }

        public void xxx()
        {
            System.Net.WebClient client = new WebClient();
            byte[] page = client.DownloadData("http://www.google.com");
            string content = System.Text.Encoding.UTF8.GetString(page);
            string regex = "href=[\\\"\\\'](http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?[\\\"\\\']";
            Regex re = new Regex(regex);
            MatchCollection matches = re.Matches(content);

            System.Collections.IEnumerator enu = matches.GetEnumerator();
            while (enu.MoveNext() && enu.Current != null)
            {
                Match match = (Match)(enu.Current);
                //Console.Write(match.Value + "\r\n");
            }
        }
        public ActionResult TestPlayer(int VoteNumberId)
        {
            //ViewBag.Model=
            return PartialView();
        }
    }
}